package com.sx.pipeline.ui.linepipe

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemLongClickListener
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.databinding.FragTerritoryListBinding
import com.sx.pipeline.repos.http.res.TerritoryVO
import com.sx.pipeline.repos.local.db.table.PipeFileBeanDO
import com.sx.pipeline.ui.linepipe.adapter.PipeLineListAdapter
import com.sx.pipeline.ui.territory.adapter.TerritoryListAdapter

/**
 * @Author: YY
 * @Date: 2022/8/3
 * @Desc: 区域列表
 */
class LinePipeTypeFragment: CYDataBindingBaseFragment<FragTerritoryListBinding, LinePipeVM>(R.layout.frag_territory_list) {

    private var position: Int = 0

    private lateinit var territoryAdapter: PipeLineListAdapter

    companion object {
        fun newInstance(position: Int) =
            LinePipeTypeFragment().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.ID, position)
                }
            }
    }

    override fun initParam() {
        super.initParam()
        if (arguments != null) {
            arguments?.let {
                position = it.getInt(IntentKey.ID)
            }
        }
    }

    override fun initListener() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onRefresh(refreshLayout: RefreshLayout) {
                mViewModel.getFileBeanList(position)
                mBinding.refreshLayout.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {

            }
        })

    }

    override fun initData() {
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getFileBeanList(position)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.deleteTerritoryEvent) {
            mViewModel.getFileBeanList(position)
        }

        setTerritoryList()
    }

    private fun setTerritoryList() {
        observe(mViewModel.territoryListEvent) {
            if (it != null && it.isNotEmpty()) {
                mBinding.emptyView.visibility = View.GONE
                mBinding.refreshLayout.setEnableLoadMore(false)
//                if (!this::territoryAdapter.isInitialized) {
//
//                }
                mBinding.rvTerritory.layoutManager = LinearLayoutManager(
                    requireContext(), LinearLayoutManager.VERTICAL, false
                )
                territoryAdapter = PipeLineListAdapter(it as MutableList<PipeFileBeanDO>)
                mBinding.rvTerritory.adapter = territoryAdapter

                territoryAdapter.setOnItemLongClickListener(object : OnItemLongClickListener {
                    override fun onItemLongClick(
                        adapter: BaseQuickAdapter<*, *>,
                        view: View,
                        position: Int
                    ): Boolean {
                        val territoryVO: PipeFileBeanDO = adapter.data[position] as PipeFileBeanDO
                        showDeleteDialog(territoryVO)
                        return true
                    }
                })

                territoryAdapter.setOnItemClickListener { adapter, view, position ->
                    val territoryVO: PipeFileBeanDO = adapter.data[position] as PipeFileBeanDO
                    RouteManager.LinePipe.intentToLinePipeDetails(position, territoryVO)
                }
            } else {
                mBinding.refreshLayout.setEnableLoadMore(false)
                mBinding.rvTerritory.adapter = null
                mBinding.emptyView.visibility = View.VISIBLE
            }
        }
    }

    /**
     * 长按删除
     */
    private fun showDeleteDialog(territoryVO: PipeFileBeanDO ) {
        XPopup.Builder(requireContext())
            .asConfirm("提示",
                "是否删除《${territoryVO.fileName}》文件？"
            ) {
                mViewModel.deleteFileBean(territoryVO)
            }.show()
    }
}